package cn.sjx.auth.controller;

import cn.sjx.auth.annotation.Permission;
import cn.sjx.auth.domain.Permission1;
import cn.sjx.auth.query.PermissionQuery;
import cn.sjx.auth.service.PermissionService;
import cn.sjx.basic.utils.AjaxResult;
import cn.sjx.basic.utils.PageList;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Permission(name="权限管理")
@Api(value = "权限API",description = "权限相关的CRUD功能")
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @Permission(name = "分页查询")
    @PostMapping
    public AjaxResult pageList(@RequestBody PermissionQuery permissionQuery) {
        PageList<Permission1> pageList =  permissionService.pageList(permissionQuery);
        return AjaxResult.me().setData(pageList);
    }


    @Permission(name = "查询权限树")
    @GetMapping("/tree")
    public AjaxResult tree() {
        try {
            List<Permission1> list = permissionService.tree();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    @Permission(name = "获取所有权限")
    @GetMapping("/sns")
    public AjaxResult getPermissionSns() {
        try {
            List<String> list = permissionService.getPermissionSns();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

}
